/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.JTextComponent;

import org.apache.log4j.Logger;

import who.tmap.bo.Annotations;
import who.tmap.bo.cd.CDColorPattern;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.bo.cd.ColorPatternClassification;
import who.tmap.toolkit.impl.AnnotationsHelper;
import who.tmap.ui.util.ColorPatternLegendTableModel;
import who.tmap.ui.util.MiscUIHelper;

import javax.swing.JCheckBox;

/**
 * A Dialog to edit color pattern legend
 * @author stl
 *
 */
public class JDialogEditColorPatternLegend extends JDialog {

    private static Logger logger = Logger.getLogger( JDialogEditColorPatternLegend.class );	
	
    private Annotations annotations;
    private List<String> legends;
    
    private ColorPatternLegendTableModel colorPatternTableModel;
	private JPanel jContentPane = null;

	private JPanel jPanelButtons = null;
	private JPanel jPanelBoundaryFormat = null;
	private JPanel jPanelControls = null;	

	private JScrollPane jScrollPane = null;

	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;
	private JButton jButtonReset = null;

	private JTable jTableColorPattern = null;

	private JCheckBox jCheckBoxRoundRangesBoundaries = null;
	
	private CartographicDisplay cd;

	/**
	 * This is the default constructor
	 */
	public JDialogEditColorPatternLegend( Frame parent, boolean modal, Annotations annotations, CartographicDisplay cd ) {
		super( parent, modal );
		
		this.annotations =annotations;
		
		this.cd = cd;
		
		legends =new ArrayList<String>();

		for( int ct =0; ct < annotations.getColorPatternLegend().size(); ct++ ) {
			legends.add( ct, new String( annotations.getColorPatternLegend().get( ct ) ) );
		}
		
		getJCheckBoxRoundRangesBoundaries().setSelected( annotations.isColorPatternRoundRangesBoundaries() );
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(400, 250);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle( "Edit Color/pattern Legend" );
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(null);
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
			jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
			jContentPane.add(getJPanelControls(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButtonClose	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jPanelButton	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
			jPanelButtons.add(getJButtonReset(), null);
		}
		return jPanelButtons;
	}
	
	private JPanel getJPanelControls() {
		if (jPanelControls == null) {
			jPanelControls = new JPanel();
			jPanelControls.setLayout( new GridLayout(2,1,5,5) );
			jPanelControls.add( getJPanelBoundaryFormat() );
			jPanelControls.add( getJPanelButtons() );			
		}
		return jPanelControls;
	}
	
	private JPanel getJPanelBoundaryFormat() {
		if (jPanelBoundaryFormat == null) {
			jPanelBoundaryFormat = new JPanel();
			jPanelBoundaryFormat.add(getJCheckBoxRoundRangesBoundaries(), null);
		}
		return jPanelBoundaryFormat;
	}		

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTableColorPattern());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					annotations.setColorPatternLegend( legends );
					annotations.setColorPatternRoundRangesBoundaries( getJCheckBoxRoundRangesBoundaries().isSelected() );
					dispose();
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.setText("Reset to defaults");
			jButtonReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					new AnnotationsHelper().resetRanges( legends,  cd );
					colorPatternTableModel.fireTableDataChanged();
				}
			});
		}
		return jButtonReset;
	}

	/**
	 * This method initializes jTableColorPattern	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableColorPattern() {
		if (jTableColorPattern == null) {
			colorPatternTableModel =new ColorPatternLegendTableModel();
			colorPatternTableModel.setColorPatternLegend( legends );
			jTableColorPattern = new JTable( colorPatternTableModel ) {
				
				//  Select the text when the cell starts editing
				//  a) text will be replaced when you start typing in a cell
				//  b) text will be selected when you use F2 to start editing
				//  c) caret is placed at end of text when double clicking to start editing
				public Component prepareEditor(
					TableCellEditor editor, int row, int column) {
					Component c = super.prepareEditor(editor, row, column);
	 
					if (c instanceof JTextComponent) {
						((JTextField)c).selectAll();
					}
	 
					return c;
				}
				
				  /**
				   * Color every other row
				   */
				  public Component prepareRenderer( TableCellRenderer renderer
				          						  , int rowIndex, int vColIndex ) {
					  Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
					  if (rowIndex % 2 == 0 ) {
						  c.setBackground(MiscUIHelper.TABLE_CELL_BG_COLOR);
					  } else {
						  // If not shaded, match the table's background
						  c.setBackground(getBackground());
						  if ( c.getBackground().equals( c.getForeground() ) ) {
							  c.setForeground( Color.black );
						  }
					  }
					  return c;
				  }
			};
			
			jTableColorPattern.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
		}
		return jTableColorPattern;
	}
	
	public Annotations getAnnotations() {
		return annotations;
	}

	/**
	 * This method initializes jCheckBoxRoundRangesBoundaries	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxRoundRangesBoundaries() {
		if (jCheckBoxRoundRangesBoundaries == null) {
			jCheckBoxRoundRangesBoundaries = new JCheckBox();
			jCheckBoxRoundRangesBoundaries.setText("Round boundaries");
			jCheckBoxRoundRangesBoundaries.setVisible( ((CDColorPattern)cd).getClassification() != ColorPatternClassification.NON_NUMERIC );
		}
		return jCheckBoxRoundRangesBoundaries;
	}

}
